using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Kiddy.Screens
{
    class LaMaCount: GameScreen
    {
        Texture2D skyBackground;
        Texture2D groundBackground;
        Texture2D cloud1;
        Texture2D cloud2;
        Texture2D redball_Pre;
        Texture2D blueball_Next;
        SpriteFont numberFont;


        Vector2 cloud1Position;
        Vector2 cloud2Position;
        Vector2 redball_Position;
        Vector2 blueball_Position;
        Vector2 fontScale;

        private int updateBalloon;
        private int updateCloud;
        int countNumber;
        string[] listLaMa;

        public void LoadAsset()
        {
            skyBackground = Load<Texture2D>("Textures/Backgrounds/sky");
            groundBackground = Load<Texture2D>("Textures/Backgrounds/ground");
            cloud1 = Load<Texture2D>("Textures/Items/cloud1");
            cloud2 = Load<Texture2D>("Textures/Items/cloud2");
            redball_Pre = Load<Texture2D>("Textures/Items/redballoon");
            blueball_Next = Load<Texture2D>("Textures/Items/blueballoon");
            numberFont = Load<SpriteFont>("Fonts/MenuFont");


            cloud1Position = new Vector2(500, 32);
            cloud2Position = new Vector2(64, 90);
            fontScale = new Vector2(70, 150);

            listLaMa = new string[20];
            //////////////////////////////
            listLaMa[0] = "I";
            listLaMa[1] = "II";
            listLaMa[2] = "III";
            listLaMa[3] = "IV";
            listLaMa[4] = "V";
            listLaMa[5] = "VI";
            listLaMa[6] = "VII";
            listLaMa[7] = "VIII";
            listLaMa[8] = "IX";
            listLaMa[9] = "X";
            listLaMa[10] = "XI";
            listLaMa[11] = "XII";
            listLaMa[12] = "XIII";
            listLaMa[13] = "XIV";
            listLaMa[14] = "XV";
            listLaMa[15] = "XVI";
            listLaMa[16] = "XVII";
            listLaMa[17] = "XVIII";
            listLaMa[18] = "XIX";
            listLaMa[19] = "XX";
            //////////////////////////////
            updateBalloon = 1;
            updateCloud = 1;
            countNumber = 0;
            blueball_Position = new Vector2(700, 200);
            redball_Position = new Vector2(20, 200);
        }

        public override void LoadContent()
        {
            LoadAsset();
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            DrawBackground();
            DrawItems();
            float messageWidth = numberFont.MeasureString(countNumber.ToString()).X;
            //ScreenManager.SpriteBatch.DrawString(
            //    numberFont, 
            //    countNumber.ToString(), 
            //    new Vector2((ScreenManager.Game.GraphicsDevice.Viewport.Width - messageWidth) / 2, ScreenManager.Game.GraphicsDevice.Viewport.Height / 2), 
            //    Color.Blue, 
            //    0, 
            //    new Vector2(100,100), 
            //    0, 
            //    SpriteEffects.None, 
            //    0);
            ScreenManager.SpriteBatch.DrawString(
                numberFont,
                listLaMa[countNumber],
                new Vector2((ScreenManager.Game.GraphicsDevice.Viewport.Width - messageWidth) / 2, ScreenManager.Game.GraphicsDevice.Viewport.Height / 2),
                Color.Blue);

            ScreenManager.SpriteBatch.End();
            base.Draw(gameTime);
        }

        private void DrawBackground()
        {
            // Clear the background
            ScreenManager.Game.GraphicsDevice.Clear(Color.White);

            // Draw the Sky
            ScreenManager.SpriteBatch.Draw(skyBackground, Vector2.Zero, Color.White);
            ScreenManager.SpriteBatch.Draw(groundBackground, new Vector2(0, ScreenManager.Game.GraphicsDevice.Viewport.Height - groundBackground.Height), Color.White);

            // Draw Cloud #1
            ScreenManager.SpriteBatch.Draw(cloud1, cloud1Position, Color.White);

            // Draw Cloud #2
            ScreenManager.SpriteBatch.Draw(cloud2, cloud2Position, Color.White);
        }

        private void DrawItems()
        {
            //ScreenManager.Game.GraphicsDevice.Clear(Color.White);

            //ScreenManager.SpriteBatch.Draw(redball_Pre, redball_Position, Color.White);
            ScreenManager.SpriteBatch.Draw(redball_Pre, redball_Position, Color.White);
            ScreenManager.SpriteBatch.Draw(blueball_Next, blueball_Position, Color.White);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (redball_Position.Y >= ScreenManager.Game.GraphicsDevice.Viewport.Height - redball_Pre.Height || redball_Position.Y < redball_Pre.Height)
            {
                updateBalloon = -updateBalloon;
            }

            if (cloud1Position.X + cloud1.Width <= 0)
            {
                cloud1Position.X = 800;
            }
            if (cloud2Position.X + cloud2.Width <= 0)
            {
                cloud2Position.X = 800;
            }
            cloud1Position.X = cloud1Position.X - updateCloud;
            cloud2Position.X = cloud2Position.X - updateCloud;
            redball_Position.Y = redball_Position.Y + updateBalloon;
            blueball_Position.Y = blueball_Position.Y + updateBalloon;

            TouchCollection touches = TouchPanel.GetState();

            if (touches.Count > 0)
            {
                if (touches[0].Position.Y >= redball_Position.Y
                    && touches[0].Position.Y < redball_Position.Y + redball_Pre.Height / 2
                    && touches[0].Position.X <= redball_Position.X + redball_Pre.Width)
                {
                    if (countNumber >= 1)
                        countNumber--;
                }

                if (touches[0].Position.Y >= blueball_Position.Y
                    && touches[0].Position.Y < blueball_Position.Y + blueball_Next.Height / 2
                    && touches[0].Position.X >= ScreenManager.Game.GraphicsDevice.Viewport.Width - blueball_Next.Width)
                {
                    if (countNumber < 19)
                        countNumber++;
                }
            }


            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
    }
}
